﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using SMBL.Global;
using SMBL.Core;
using SMBL.ConfigElement.DBProvider;
using SMBL.Interface.Database;

namespace SMBL
{
    namespace ADBS
    {
        /// <summary>
        /// Connection Control Unit
        /// @Author: Push
        /// </summary>
        public class ConnectionControl
        {
            //////////////////////////////////////////////////////////////////////////
            // Global Parameters
            //////////////////////////////////////////////////////////////////////////
            private static Hashtable myConnectionPoolMap = new Hashtable();


            /// <summary>
            /// Private Construction
            /// This class cannot be instanced.
            /// @Author: Push
            /// </summary>
            private ConnectionControl() { }

            /// <summary>
            /// Initialize the connection pool system
            /// map the name and the connection pool
            /// @Author: Push
            /// </summary>
            public static void Initialize()
            {
                // Get all names of the Providers
                Hashtable tmpProviderList = Kernel.GetInstance().ProviderInfoList;
                ArrayList tmpProviderNames = new ArrayList(tmpProviderList.Keys);
                // Create connection pool instances
                for (int i = tmpProviderList.Count - 1; i >= 0; --i)
                {
                    ConnectionPool connPool = new ConnectionPool((DBProviderInfo)tmpProviderList[tmpProviderNames[i]]);
                    connPool.Start();
                    myConnectionPoolMap.Add(tmpProviderNames[i], connPool);
                }
            }

            /// <summary>
            /// End all connection pools
            /// @Author: Push
            /// </summary>
            public static void Terminate()
            {
                // Get all names of the Providers
                ArrayList tmpProviderNames = new ArrayList(myConnectionPoolMap.Keys);
                // End all connection pool
                for (int i = tmpProviderNames.Count - 1; i >= 0; --i)
                {
                    ((ConnectionPool)myConnectionPoolMap[tmpProviderNames[i]]).End();
                }

                myConnectionPoolMap.Clear();
            }

            /// <summary>
            /// Get Specified Connection according to
            /// the database's name sent in
            /// @Author: Push
            /// </summary>
            /// <param name="BindDBName"></param>
            /// <returns></returns>
            public static IDBProvider GetConnection(String BindDBName)
            {
                if (BindDBName == null) throw new NullReferenceException();
                // Invoke the bind connection pool's get connection method
                return ((ConnectionPool)myConnectionPoolMap[BindDBName]).GetConnection();
            }

            /// <summary>
            /// Release the used connection according to 
            /// the database's name sent in
            /// @Author: Push
            /// </summary>
            /// <param name="BindDBName"></param>
            /// <param name="UsedProvider"></param>
            public static void ReleaseConnection(String BindDBName, IDBProvider UsedProvider)
            {
                if (BindDBName == null) throw new NullReferenceException();
                // Invoke the bind connection pool's release connection method
                ((ConnectionPool)myConnectionPoolMap[BindDBName]).ReleaseConnection(UsedProvider);
            }
        }
    }
}